Introduction

In this exercise, you will practice working with SwiftUI by creating a user profile page. You will use elements like Text , Image and Button to compose a user information page that will include a title and some user details to display the overall information about the user.

Scenario

The Little Lemon restaurant needs a page to display a user’s personal information, and you were tasked with creating such a page using SwiftUI.

Instructions

Before you begin make sure you have the latest version of XCode. Use the same project that you used in previous exercises in this lesson.

Step 1: Create a new SwiftUI file for onboarding.

You should now have a file in your project file tree named UserProfile.

Step 3: Add another tab item on the home screen for the profile page

Follow the steps below to add another bottom tab bar item button to access the profile page:

If you are having trouble implementing any of the steps above, review the following resource from the "Create the User Interface in SwiftUI" course:

Step 2: Add a vertical stack and title to your profile page

Open the newly created UserProfile file. You will notice some default view code generated for you that outputs to screen “ Hello, World! ” in a Text view.

Complete the following tasks to modify it:

Save the code and you should have the text “ Personal information ” appear at the center of the screen.

If you are having trouble implementing any of the steps above, review the following resources from the "Create the User Interface in SwiftUI" course:

Step 3: Add an image to hold the profile picture

In this step you'll add the profile image by completing the following:

You should now have text at the center of the screen, followed by the profile image below it with a placeholder loaded.

If you're having trouble with any of the steps above, review the following resource from the "Create the User Interface in SwiftUI" course:

Step 4: Add three text elements to hold user information from user defaults

Add three text fields below the image inside the VStack content to hold the first name, last name and email information that the user entered during the registration and was saved in UserDefaults :

You should now have three Text elements referencing constants declared above them that hold the values from the UserDefaults .

Tip: The string method of the UserDefaults returns an Optional value, so when setting the constants to the Text elements, you will need to unwrap them first, for instance, optionalStringConstant ?? "" .

If you're having trouble with any of the steps above, review the following resource from the "Create the User Interface in SwiftUI" course:

Step 5: Add a button to logout

Use Button and the steps below to create logout logic:

If you're having trouble with any of the steps above, review the following resource from the "Create the User Interface in SwiftUI" course:

Step 6: Test the features you've created

To style your views in a more advanced way you can refer to the contents covered in the Create the User Interface with SwiftUI course, as well as view the following documentation:

Conclusion

This concludes setting up the profile page. In this exercise, you have practiced working with SwiftUI to create a user profile page for Little Lemon.